package com.easyar.customizdblibrary;

import android.content.ContentValues;
import android.text.TextUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Locale;

/**
 * Created by Sigthp-P-Department on 2018/4/11.
 */

public class DBUtile {
    public static String getColumnType(String type) {
        String value = null;
        if (type.contains("String")) {
            value = " text";
        } else if (type.contains("int")) {
            value = " integer";
        } else if (type.contains("boolean")) {
            value = " boolean";
        } else if (type.contains("float")) {
            value = " float";
        } else if (type.contains("double")) {
            value = " double";
        } else if (type.contains("char")) {
            value = " varchar";
        } else if (type.contains("long")) {
            value = " long";
        }
        return value;
    }

    public static String capitalize(String string) {
        if (!TextUtils.isEmpty(string)) {
            return string.substring(0, 1).toUpperCase(Locale.US) + string.substring(1);
        }
        return string == null ? null : "";
    }
    public static byte[] toByteArray (Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray ();
            oos.close();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return bytes;
    }

    public static void setContentValue(ContentValues contentValues, Object obj,String name,String type) {
        if (type.contains("String")) {
            String value = obj+"";
            contentValues.put(name,value);
        } else if (type.contains("int")) {
            String s = obj+"";
            int value = Integer.parseInt(s);
            contentValues.put(name,value);
        } else if (type.contains("boolean")) {
            String s = obj+"";
            boolean value = Boolean.parseBoolean(s);
            contentValues.put(name,value);
        } else if (type.contains("float")) {
            String s = obj+"";
            float value = Float.parseFloat(s);
            contentValues.put(name,value);
        } else if (type.contains("double")) {
            String s = obj+"";
            double value = Double.parseDouble(s);
            contentValues.put(name,value);
        } else if (type.contains("short")) {
            String s = obj+"";
            short value = Short.parseShort(s);
            contentValues.put(name,value);
        } else if (type.contains("long")) {
            String s = obj+"";
            Long value = Long.parseLong(s);
            contentValues.put(name,value);
        }else if(type.contains("byte")){
            String s = obj+"";
            Byte value = Byte.parseByte(s);
            contentValues.put(name,value);
        }else if(type.contains("byte[]")){
            byte[] value = DBUtile.toByteArray(obj);
            contentValues.put(name,value);
        }
    }
}
